package com.hpxx.ylzswl.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hpxx.ylzswl.R;
import com.hpxx.ylzswl.activity.OrderSearchActivity;
import com.hpxx.ylzswl.adapter.HoemPagerFragmentAdapter;
import com.hpxx.ylzswl.base.BaseEvent;
import com.hpxx.ylzswl.base.BaseFragment;
import com.hpxx.ylzswl.event.OrderTabEvent;
import com.hpxx.ylzswl.https.HttpAddress;
import com.hpxx.ylzswl.https.RequestParams;
import com.hpxx.ylzswl.utils.ConstantsUtils;
import com.hpxx.ylzswl.views.CustomViewPager;
import com.universal.frame.generalutils.GeneralUtil;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.Subscribe;
import okhttp3.Request;

/**
 * Created by Administrator on 2017/11/7.
 */
public class OrderFragment extends BaseFragment implements View.OnClickListener,ResultCallBack{
    private int state;
    private List<BaseFragment> fragmentList = new ArrayList<BaseFragment>();
    private TextView tv_djd,tv_yjd,tv_wc;
    private View line_djd,line_yjd,line_wc;
    private CustomViewPager cvp_viewpager;
    private HoemPagerFragmentAdapter pagerAdapter;
    private LinearLayout ll_search_jg;
  private ImageView iv_red;

    public static OrderFragment newInstance(int state) {
        OrderFragment fragment = new OrderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ConstantsUtils.FRAGMENT_FLG, state);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.state = getArguments().getInt(ConstantsUtils.FRAGMENT_FLG);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_order;

    }

    @Override
    public void initView(View view) {
        cvp_viewpager = view.findViewById(R.id.cvp_viewpager);
        iv_red = view.findViewById(R.id.iv_red);
        tv_djd = view.findViewById(R.id.tv_djd);
        tv_yjd = view.findViewById(R.id.tv_yjd);
        tv_wc = view.findViewById(R.id.tv_wc);
        line_djd = view.findViewById(R.id.line_djd);
        line_yjd = view.findViewById(R.id.line_yjd);
        line_wc = view.findViewById(R.id.line_wc);
        ll_search_jg = view.findViewById(R.id.ll_search_jg);

        tv_djd.setOnClickListener(this);
        tv_yjd.setOnClickListener(this);
        tv_wc.setOnClickListener(this);
        ll_search_jg.setOnClickListener(this);
        itemPost();
        select(0);
        initViewPager();
    }

    private void initViewPager(){
        fragmentList.add(OrderItemFragment.newInstance(0));
        fragmentList.add(OrderItem2Fragment.newInstance(1));
        fragmentList.add(OrderItem3Fragment.newInstance(2));
        pagerAdapter = new HoemPagerFragmentAdapter(getActivity().getSupportFragmentManager(),getActivity(),fragmentList);
        cvp_viewpager.setAdapter(pagerAdapter);
        cvp_viewpager.setOffscreenPageLimit(fragmentList.size());
        cvp_viewpager.setCurrentItem(0,false);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_djd:
                select(0);
                cvp_viewpager.setCurrentItem(0,false);
                break;

            case R.id.tv_yjd:
                select(1);
                cvp_viewpager.setCurrentItem(1,false);
                break;

            case R.id.tv_wc:
                select(2);
                cvp_viewpager.setCurrentItem(3,false);
                break;

            case R.id.ll_search_jg:
                startActivity(new Intent(getActivity(), OrderSearchActivity.class));
                break;

            default:
                break;
        }
    }

    private void select(int flg){
        switch (flg){
            case 0:
                tv_djd.setTextColor(Color.parseColor("#E1524A"));
                line_djd.setBackgroundResource(R.color.mian_color);
                line_djd.setVisibility(View.VISIBLE);
                tv_yjd.setTextColor(Color.parseColor("#666666"));
                line_yjd.setVisibility(View.INVISIBLE);
                tv_wc.setTextColor(Color.parseColor("#666666"));
                line_wc.setVisibility(View.INVISIBLE);
                break;

            case 1:
                tv_djd.setTextColor(Color.parseColor("#666666"));
                line_djd.setVisibility(View.INVISIBLE);
                tv_yjd.setTextColor(Color.parseColor("#E1524A"));
                line_yjd.setBackgroundResource(R.color.mian_color);
                line_yjd.setVisibility(View.VISIBLE);
                tv_wc.setTextColor(Color.parseColor("#666666"));
                line_wc.setVisibility(View.INVISIBLE);
                break;

            case 2:
                tv_djd.setTextColor(Color.parseColor("#666666"));
                line_djd.setVisibility(View.INVISIBLE);
                tv_yjd.setTextColor(Color.parseColor("#666666"));
                line_yjd.setVisibility(View.INVISIBLE);
                tv_wc.setTextColor(Color.parseColor("#E1524A"));
                line_wc.setBackgroundResource(R.color.mian_color);
                line_wc.setVisibility(View.VISIBLE);
                break;

            default:
                break;
        }
    }
    //检查项目TAb红点
    private void itemPost(){
        OKHttpUtils.postAsync(getContext()
                , HttpAddress.UPLOAD_TAB_URL
                , new RequestParams(getActivity()).getUploadTabParams(), this
                ,false,1);
    }

    @Override
    public void onSuccess(String response, int flag) {
        int status = JsonUtil.getInt(response, "status", 0);
        try {
            switch (flag) {
                case 1:
                    itemDetail(response);
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            GeneralUtil.tryShow(getContext(), e);
        }
    }

    private void itemDetail(String response) {
        int status = JsonUtil.getInt(response, "status", 0);
        String msg = JsonUtil.getString(response,"msg","");
        if (status == 1){
            String data = JsonUtil.getString(response, "data", "");
            String tabRead = JsonUtil.getString(data,  "tabRead", "");
            if ("0".equals(tabRead)){
                iv_red.setVisibility(View.VISIBLE);
            }else {
                iv_red.setVisibility(View.GONE);
            }
        }else {
            ToastUtil.showToast(getContext(),msg);
        }
    }

    @Override
    public void onFailure(Request request, IOException e, int flag) {
        iv_red.setVisibility(View.GONE);
    }
    @Override
    @Subscribe
    public void onEventMainThread(BaseEvent event) throws IOException {
        super.onEventMainThread(event);

        if (event instanceof OrderTabEvent) {
            itemPost();
        }
    }
}
